﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoastalClimbing.Code;
using System.Data;
using System.Collections;

namespace CoastalClimbing
{
    public partial class Wall : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                this.BindInfo();
        }

        private void BindInfo()
        {
            string wallid = string.Empty;
            DataTable routeInfo = null;

            try
            {
                wallid = Request.QueryString["id"].ToString();
                string wallname = DAL.WallSelectName(wallid);
                lblWallName.Text = wallname;
                Page.Title = "Coastal Climbing - " + wallname;

            }
            catch 
            {
                //throw ex;
            }
            try
            {
                string imageName = string.Format("~/Images/{0}", DAL.GetWallImage(wallid));

                if (!string.IsNullOrWhiteSpace(imageName) && System.IO.File.Exists(Server.MapPath(imageName)))
                {
                    imgWall.ImageUrl = imageName;
                }
            }
            catch 
            {
              //  throw ex;
            }
            try
            {

                routeInfo = DAL.RoutesSelect(wallid);

                grvRoutes.DataSource = routeInfo;
                grvRoutes.DataBind();
            }
            catch 
            {
                //throw ex;
            }
            try
            {

                EnumerableRowCollection<DataRow> rows = routeInfo.AsEnumerable();

                var result = (from route in rows orderby route["DATEADDED"] descending select route).Take(1);
                hyperNewestRoute.Text = result.ToList()[0]["NAME"].ToString();
                hyperNewestRoute.NavigateUrl = string.Format("~/Route.aspx?id={0}", result.ToList()[0]["ID"].ToString());

                var ratings = (from route in rows orderby route["RATING"] descending select route);

                lblHardGrade.Text = ratings.First()["RATING"].ToString();
                lblEasyGrade.Text = ratings.Last()["RATING"].ToString();

                int avgGrade = 0;

                foreach (DataRow row in ratings)
                {
                    avgGrade += Convert.ToInt32(row["RATING"].ToString().Substring(1));
                }

                lblAvgGrade.Text = string.Concat("V", (avgGrade / ratings.Count()).ToString());
            }
            catch 
            {
                //throw ex;
            }
            try
            {

                DataTable chartInfo = DAL.GetChartData(wallid);

                List<DictionaryEntry> data = new List<DictionaryEntry>();

                foreach (DataRow row in chartInfo.Rows)
                {
                    data.Add(new DictionaryEntry(row["RATING"].ToString(), row["NUMOF"].ToString()));
                }

                if(data != null && data.Count > 0)
                {
                    chtRoutes.Visible = true;
                    chtRoutes.Series["Routes"].Points.DataBindXY(data, "KEY", data, "VALUE");
                }
                else
                {
                    chtRoutes.Visible = false;
                }
            }
            catch{}
        }
    }
}
